using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TheGameToEndAllGames {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class World : Microsoft.Xna.Framework.Game {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D baseTileTexture;
        Texture2D grassTileTexture;

        List<Actor> actors = new List<Actor>();

        public World() {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 500;
            graphics.PreferredBackBufferWidth = 650;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize() {
            for (int i = 0; i < 10; i++) {
                addActor(new GrassTile(), 0, 0 + 50*i);
            }
            for (int i = 0; i < 12; i++) {
                addActor(new GrassTile(), 50 + 50*i, 0);
            }
            for (int i = 0; i < 12; i++) {
                addActor(new GrassTile(), 50 + 50*i, 450);
            }
            for (int i = 0; i < 8; i++) {
                addActor(new GrassTile(), 600, 50 + 50*i);
            }
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 50, 50 + 50 * i);
            }
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 150, 50 + 50 * i);
            } 
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 250, 50 + 50 * i);
            } 
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 350, 50 + 50 * i);
            }
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 450, 50 + 50 * i);
            }
            for (int i = 0; i < 8; i++) {
                addActor(new BaseTile(), 550, 50 + 50 * i);
            }
            addActor(new BaseTile(), 100, 400);
            addActor(new BaseTile(), 200, 50);
            addActor(new BaseTile(), 300, 400);
            addActor(new BaseTile(), 400, 50);
            addActor(new BaseTile(), 500, 400);
            for (int i = 0; i < 7; i++) {
                addActor(new GrassTile(), 100, 50 + 50 * i);
            }
            for (int i = 0; i < 7; i++) {
                addActor(new GrassTile(), 200, 100 + 50 * i);
            }
            for (int i = 0; i < 7; i++) {
                addActor(new GrassTile(), 300, 50 + 50 * i);
            }
            for (int i = 0; i < 7; i++) {
                addActor(new GrassTile(), 400, 100 + 50 * i);
            }
            for (int i = 0; i < 7; i++) {
                addActor(new GrassTile(), 500, 50 + 50 * i);
            }

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent() {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            baseTileTexture = Content.Load<Texture2D>("BaseTile");
            grassTileTexture = Content.Load<Texture2D>("GrassTile");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent() {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime) {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin();
            foreach(Actor a in actors) {
                int x = (int)a.GetX();
                int y = (int)a.GetY();
                Texture2D texture = GetTextureOfClass(a);
                if (texture != null) {
                    spriteBatch.Draw(texture, new Vector2(x, y), Color.White);
                }
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        private Texture2D GetTextureOfClass(Actor a) {
            if (a is GrassTile) {
                return grassTileTexture;
            }
            if (a is BaseTile) {
                return baseTileTexture;
            }
            return null;
        }

        public void addActor(Actor a, int posX, int posY) {
            if (!actors.Contains<Actor>(a)) {
                a.SetLocation(posX, posY);
                actors.Add(a);
            }
        }
    }
}
